﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pc_system.Model; 

namespace pc_system.View
{
    public partial class ProductInfo : Form
    {
        public ProductInfo()
        {
            InitializeComponent();
        }
        private float cost;
        public float Cost
        {
            set { this.cost = value;}
            get { return this.cost;}
        }
        private int type = 0;

        public ProductInfo(string bcode)
        {
            InitializeComponent();
            this.tbBarcode.Text = bcode;
            btnSubmit.Click += AddNewProduct_Click;
        }
        public ProductInfo(string bcode, int type)
        {
            InitializeComponent();
            this.type = 1;
            this.tbBarcode.Text = bcode;
            btnSubmit.Click += AddNewProduct_Click;
            进货量.Visible = false;
            tbPdCount.Visible = false;
        }

        public ProductInfo(string bcode, string name, float saleprice)
        {
            InitializeComponent();
            this.tbBarcode.Text = bcode;
            this.tbPdName.Text = name;
            this.tbSalePrice.Text = saleprice.ToString();
            this.tbSalePrice.Enabled = false;
            this.tbPdName.Enabled = false;
            btnSubmit.Click += AddProductCostPrice_Click;
            进货量.Visible = false;
            tbPdCount.Visible = false;
        }

        public ProductInfo(string bcode, string name)
        {
            InitializeComponent();
            this.tbBarcode.Text = bcode;
            this.tbPdName.Text = name;
            btnSubmit.Click += AddProductSalePrice_Click;
            进货量.Visible = false;
            tbPdCount.Visible = false;
        }



        private void AddNewProduct_Click(object sender, EventArgs e)
        {
            float costprice, saleprice;
            int count;
            if (!checkTextBox()) MessageBox.Show("输入有错，请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
            try
            {
                costprice = float.Parse(tbCostPrice.Text);
                saleprice = float.Parse(tbSalePrice.Text);
                if (!DBFunc.addNewProductInfo(tbBarcode.Text, tbPdName.Text)) return;
                if (type == 0)
                {
                    if (!int.TryParse(tbPdCount.Text, out count)) return;
                    if (!DBFunc.addNewBuyRecord(tbBarcode.Text, count, costprice)) return;
                }
                if (!DBFunc.addNewProductOnSale(tbBarcode.Text, saleprice)) return;
                SystemInfo f = new SystemInfo("添加成功");
                f.ShowDialog();
                this.Cost = costprice;
                this.DialogResult = DialogResult.OK;
            } 
            catch (Exception)
            {
                
            }
        }

        private void AddProductSalePrice_Click(object sender, EventArgs e)
        {
            float costprice, saleprice;
            if (!checkTextBox()) MessageBox.Show("输入有错，请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
            if (!float.TryParse(tbCostPrice.Text, out costprice)) return;
            if (!float.TryParse(tbSalePrice.Text, out saleprice)) return;
            if (!DBFunc.addNewProductOnSale(tbBarcode.Text, saleprice)) return;
            this.Cost = costprice;
            this.DialogResult = DialogResult.OK;
        }

        private void AddProductCostPrice_Click(object sender, EventArgs e)
        {
            float costprice;
            if (!checkTextBox()) MessageBox.Show("输入有错，请重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (!float.TryParse(tbCostPrice.Text, out costprice)) return;
            this.Cost = costprice;
            this.DialogResult = DialogResult.OK;
        }
        #region 快捷键
        //重写处理命令键方法以监听快捷键
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                //关闭添加商品窗口
                this.DialogResult = DialogResult.Abort;
            }
            if (keyData == Keys.Enter)
            {
                this.btnSubmit.PerformClick();
                return base.ProcessCmdKey(ref msg, keyData);
            }

            //继续原来base.ProcessCmdKey中的处理
            return base.ProcessCmdKey(ref msg, keyData);
        }


        #endregion


        private void textbox_MouseClick(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.SelectAll();
        }

        /// <summary>
        /// 检查字段
        /// </summary>
        /// <returns></returns>
        private bool checkTextBox()
        {
            TypeChecker chk = new TypeChecker();
            if (chk.IsInt(tbBarcode.Text) &&
                tbCostPrice.Visible && chk.IsReal(tbCostPrice.Text) &&
                tbSalePrice.Visible && chk.IsReal(tbSalePrice.Text) &&
                tbPdCount.Visible && chk.IsInt(tbPdCount.Text))
            {
                return true;
            }
            return false;
        }
    }
}
